//
//  WaitTaskCell.m
//  RRQS
//
//  Created by kyjun on 16/3/26.
//
//

#import "WaitTaskCell.h"

@interface WaitTaskCell ()
@property(nonatomic,strong) UIButton* btnIcon;
@property(nonatomic,strong) UILabel* labelContent;
@property(nonatomic,strong) UILabel* labelDistance;
@property(nonatomic,strong) UIImageView* line;
@end

@implementation WaitTaskCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor colorWithRed:255/255.f green:255/255.f blue:255/255.f alpha:1.0];
        [self layoutUI];
        [self layoutConstraints];
    }
    return self;
}

#pragma mark =====================================================  user interface layout
-(void)layoutUI{
    [self addSubview:self.btnIcon];
    [self addSubview:self.labelDistance];
    [self addSubview:self.labelContent];
    [self addSubview:self.line];
}

-(void)layoutConstraints{
    self.btnIcon.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelDistance.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelContent.translatesAutoresizingMaskIntoConstraints = NO;
    self.line.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.btnIcon addConstraint:[NSLayoutConstraint constraintWithItem:self.btnIcon attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:25.f]];
    [self.btnIcon addConstraint:[NSLayoutConstraint constraintWithItem:self.btnIcon attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:25.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.btnIcon attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.btnIcon attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    [self.labelDistance addConstraint:[NSLayoutConstraint constraintWithItem:self.labelDistance attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/4]];
    [self.labelDistance addConstraint:[NSLayoutConstraint constraintWithItem:self.labelDistance attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:25.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelDistance attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelDistance attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelContent attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelContent attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.btnIcon attribute:NSLayoutAttributeRight multiplier:1.0 constant:10.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelContent attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.labelContent attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.labelDistance attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.line addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:1.0f]];
    [self.line addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
}

#pragma mark =====================================================  property package
- (void)setWaiMaiDodelHList:(WJWaiMaiDodelHList *)waiMaiDodelHList
{
    if(waiMaiDodelHList){
        _waiMaiDodelHList = waiMaiDodelHList;
        NSString*strContent = [NSString stringWithFormat:@"%@ \n%@",waiMaiDodelHList.title,waiMaiDodelHList.get_address];
        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strContent];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.f],NSForegroundColorAttributeName:[UIColor colorWithRed:0/255.f green:0/255.f blue:0/55.f alpha:1.0]} range:[strContent rangeOfString:waiMaiDodelHList.title]];
        
        NSString* strDistance = [NSString stringWithFormat:@"%@km取货",waiMaiDodelHList.location_get];
        NSMutableAttributedString* attributeStr2  = [[NSMutableAttributedString alloc]initWithString:strDistance];
        [attributeStr2 addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strDistance rangeOfString:[NSString stringWithFormat:@"%@km",waiMaiDodelHList.location_get]]];
        
        self.btnIcon.selected = YES;
        _labelContent.attributedText = attributeStr;
        _labelDistance.attributedText = attributeStr2;
        [_btnIcon setImage:[UIImage imageNamed:@"car@2x"] forState:UIControlStateSelected];
    }
}
- (void)setWaiMaiDodelListitems:(WJWaiMaiDodelListitems *)waiMaiDodelListitems
{
    if(waiMaiDodelListitems){
        _waiMaiDodelListitems = waiMaiDodelListitems;
        NSString*strContent = [NSString stringWithFormat:@"%@ \n%@",waiMaiDodelListitems.site_name,waiMaiDodelListitems.site_addr];
        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strContent];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.f],NSForegroundColorAttributeName:[UIColor colorWithRed:0/255.f green:0/255.f blue:0/55.f alpha:1.0]} range:[strContent rangeOfString:waiMaiDodelListitems.site_name]];
        
        NSString* strDistance = [NSString stringWithFormat:@"%@km取货",waiMaiDodelListitems.location];
        NSMutableAttributedString* attributeStr2  = [[NSMutableAttributedString alloc]initWithString:strDistance];
        [attributeStr2 addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strDistance rangeOfString:[NSString stringWithFormat:@"%@km",waiMaiDodelListitems.location]]];
        
        self.btnIcon.selected = YES;
        _labelContent.attributedText = attributeStr;
        _labelDistance.attributedText = attributeStr2;
        [_btnIcon setImage:[UIImage imageNamed:@"icon-sendStatus-enter"] forState:UIControlStateSelected];
    }
}

-(void)setEntity:(MSubOrder *)entity{
    if(entity){
        _entity = entity;
        NSString*strContent = [NSString stringWithFormat:@"%@ \n%@",entity.storeName,entity.storeAddress];
        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strContent];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.f],NSForegroundColorAttributeName:[UIColor colorWithRed:0/255.f green:0/255.f blue:0/55.f alpha:1.0]} range:[strContent rangeOfString:entity.storeName]];
       
       NSString* strDistance = [NSString stringWithFormat:@"%@km取货",entity.location];
       NSMutableAttributedString* attributeStr2  = [[NSMutableAttributedString alloc]initWithString:strDistance];
        [attributeStr2 addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strDistance rangeOfString:[NSString stringWithFormat:@"%@km",entity.location]]];
        
        if([entity.sendStatus isEqualToString:@"1"]){
            self.btnIcon.selected = YES;
            self.labelDistance.hidden = NO;
        }else {
            if (entity.status == expressStatusIng ){
                self.btnIcon.selected = NO;
                self.labelContent.text = strContent;
                self.labelDistance.text = @"已取货";
                
            }else if(entity.status == expressStatusWating ){
                self.btnIcon.selected = YES;
                 _labelContent.attributedText = attributeStr;
                _labelDistance.attributedText = attributeStr2;
            }else if (entity.status == expressStatusFinished ){
                self.btnIcon.selected = NO;
            }else if (entity.status == expressStatusCancelled || entity.status == expressStatusCanceling ){
                self.labelContent.text = strContent;
                self.btnIcon.selected = NO;
                self.labelDistance.text = @"已取消";
            }
        }
    }
}

-(UIButton *)btnIcon{
    if(!_btnIcon){
        _btnIcon = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_btnIcon setImage:[UIImage imageNamed:@"icon-sendStatus-default"] forState:UIControlStateNormal];
//        [_btnIcon setImage:[UIImage imageNamed:@"icon-sendStatus-enter"] forState:UIControlStateSelected];
    }
    return _btnIcon;
}

-(UILabel *)labelContent{
    if(!_labelContent){
        _labelContent = [[UILabel alloc]init];
        _labelContent.font = [UIFont systemFontOfSize:14.f];
        _labelContent.numberOfLines = 0;
        _labelContent.lineBreakMode = NSLineBreakByCharWrapping;
        _labelContent.textColor =  [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];
    }
    return _labelContent;
}

-(UILabel *)labelDistance{
    if(!_labelDistance){
        _labelDistance = [[UILabel alloc]init];
        _labelDistance.font = [UIFont systemFontOfSize:12.f];
        _labelDistance.textColor =  [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];;
        _labelDistance.textAlignment = NSTextAlignmentRight;
    }
    return _labelDistance;
}

-(UIImageView *)line{
    if(!_line){
        _line = [[UIImageView alloc]init];
        _line.backgroundColor =  [UIColor colorWithRed:231/255.0f green:231/255.0f blue:231/255.0f alpha:1.0f];
    }
    return _line;
}
@end
